<template>
  <!-- <img alt="Vue logo" src="./assets/logo.png" /> -->

  <router-view></router-view>
  <!-- <div class="loading" v-show="$store.state.isLoading">
     <img src="./assets/images/loading.gif" alt="">
   </div> -->
</template>
<style>
@import "./assets/css/reset.css";
@import "./assets/css/iconfont.css";
</style>
<script>
import { onMounted } from "vue";
import { request } from "/@/network/request.js";
import { Toast } from "vant";
import {HOSTURL} from "./setting.js"
export default {
  name: "App",
  components: {},
  setup() {
    const REDIRECT_URI = encodeURIComponent("http://photobook.zhongaotech.com");
    const login = () => {
      window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx73a44bbd78e9cf99&redirect_uri=${REDIRECT_URI}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
    };
    const WxLogin = async () => {
      let address = window.location.href.split("#/")[1];
      if (window.location.search && window.sessionStorage.getItem("openid")==undefined) {
        let code = window.location.search.substring(1).split("&");
        for (let i = 0; i < code.length; i++) {
          let need = code[i].split("=");
          if (need[0] == "code" && need[1]) {
            var params = new URLSearchParams();
            params.append("code", need[1]);
            const res = await request({
              url: "v1.ucenter/getOpenid",
              data: params,
            });
            sessionStorage.setItem("openid", res.data.openid);
            sessionStorage.setItem("access_token", res.data.access_token);
            if (res.code === 200) {
              const result = await request({
                url: `v1.ucenter/getWechatUserInfo`,
                data: {
                  access_token:sessionStorage.getItem("access_token"),
                  openid: res.data.openid || sessionStorage.getItem("openid"),
                },
              });
              if (result.code === 200) {
                const { headimgurl, nickname } = result.data.data;
                sessionStorage.setItem("headimgurl",headimgurl)
                sessionStorage.setItem("nickname",nickname)
                const res2 = await request({
                  url: "v1.ucenter/doLogin",
                  data: {
                    headimgurl,
                    nickname,
                    openid: res.data.openid,
                  },
                });
                if (res2.code === 200) {
                  sessionStorage.setItem("user_id",res2.data.user.id);
                  // 跳转当前页面
                  window.location.href = HOSTURL +"#/home";
                } else {
                  Toast(res2.msg);
                  // 添加的代码
                }
              } else {
                Toast(result.msg);
              }
            } else {
              console.log("getOpenid  code不为200");
              Toast(res.msg);
            }
            return;
          } else {
            login();
          }
        }
      } else if (!window.sessionStorage.getItem("openid")) {
        login();
      }
    };
    onMounted(() => {
      WxLogin();
    });
    
  },
};
</script>
